<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="公众号类型">
          <a-radio-group v-decorator="['accountType',{'initialValue':2}]">
            <a-radio :value="0">服务号</a-radio>
            <a-radio :value="1">订阅号</a-radio>
            <a-radio :value="2">小程序</a-radio>
            <a-radio :value="3">企业号</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="微信公众号名称">
          <a-input placeholder="请输入微信公众号名称" v-decorator="['accountName',validatorRules.accountName]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="微信APPID">
          <a-input placeholder="请输入微信APPID" v-decorator="['accountAppid',validatorRules.accountAppid]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="微信APPSECRET">
          <a-input placeholder="请输入微信APPSECRET" v-decorator="['accountAppsecret',validatorRules.accountAppsecret]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="微信原始ID">
          <a-input placeholder="请输入微信原始ID" v-decorator="['accountWxid',validatorRules.accountWxid]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="内部编号">
          <a-input placeholder="请输入内部编号" v-decorator="['accountCode',validatorRules.accountCode]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="公众号状态">
          <a-radio-group v-decorator="['isEnable',{'initialValue':1}]">
            <a-radio :value="0">启用</a-radio>
            <a-radio :value="1">停用</a-radio>
          </a-radio-group>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {duplicateCheck} from '@/api/api'

  export default {
    name: "WeixinAccountModal",
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        id: '',//数据id。
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          accountName: {rules: [{required: true, message: '请输入微信公众号名称!'}]},
          accountAppid: {rules: [{required: true, message: '请输入微信APPID!'}, {validator: this.validateAppid}]},
          accountAppsecret: {rules: [{required: true, message: '请输入微信APPSECRET!'}]},
          accountWxid: {rules: [{required: true, message: '请输入微信原始ID!'}]},
          accountCode: {rules: [{required: true, message: '请输入内部编号!'},{validator: this.validateAccountCode}]}
        },
        url: {
          add: "/weixin/weixinAccount/add",
          edit: "/weixin/weixinAccount/edit",
        },
      }
    },
    created() {
    },
    methods: {
      //验证内部编号
      validateAccountCode(rule, value, callback) {
        var params = {
          tableName: 'weixin_account',
          fieldName: 'account_code',
          fieldVal: value,
          dataId: this.id
        };
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback("内部编号已存在!")
          }
        })
      },
      //验证appid
      validateAppid(rule, value, callback) {
        var params = {
          tableName: 'weixin_account',
          fieldName: 'account_appid',
          fieldVal: value,
          dataId: this.id
        };
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback("微信APPID已存在!")
          }
        })
      },
      add() {
        this.edit({});
      },
      //编辑
      edit(record) {
        this.form.resetFields();
        this.id = record.id;
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'accountName','isEnable', 'accountType', 'accountAppid', 'accountAppsecret', 'accountWxid', 'accountCode'))
          //时间格式化
        });
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
    }
  }
</script>

<style lang="less" scoped>

</style>